package com.han.hotelplat.service.bill.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.han.hotelplat.comm.Constant;
import com.han.hotelplat.comm.utils.ListSortUtil;
import com.han.hotelplat.dao.bill.BillDao;
import com.han.hotelplat.dao.wallet.WalletDao;
import com.han.hotelplat.pojo.bill.Bill;
import com.han.hotelplat.pojo.wallet.Wallet;
import com.han.hotelplat.service.bill.BillService;
import com.han.hotelplat.service.wallet.WalletService;

@Service
public class BillServiceImpl implements BillService{
	@Autowired
	private BillDao billDao;
	@Autowired
	private WalletService walletService;
	@Autowired
	private WalletDao walletDao;
	
	@Override
	public List<Bill> findMyBill(String userid) {
		Wallet mywallet = walletService.getWallet(userid);
		List<Bill> total = new ArrayList<Bill>();
		//查询出账账单
		List<Bill> expbilllist = billDao.findByProperty("expwalletid", mywallet.getId());
		//查询入账账单
		List<Bill> inbilllist = billDao.findByProperty("inwalletid", mywallet.getId());
		for(Bill bill:expbilllist){
			bill.setAmount(-1*bill.getAmount());//出账账单金额为负
			bill.setBalance(bill.getExpbalance());
			total.add(bill);
		}
		for(Bill bill:inbilllist){
			bill.setBalance(bill.getInbalance());
			total.add(bill);
		}
		ListSortUtil<Bill> sortLi = new ListSortUtil<Bill>();
		sortLi.sort(total, "createtime", false);
		return total;
	}

	@Override
	public Map<String, Object> creatBill(Integer expwalletid,
			Integer inwalletid, Float money, Integer type, String remark,
			String taskid, Integer chiltaskid, Integer chargechannel, 
			String optUserid) {
		Map<String, Object>result = new HashMap<String, Object>();
		Wallet expwallet = null;
		Wallet inwallet = null;
		Bill bill = new Bill();
		if(money<0){//减少余额(后台修改的)
			inwalletid = null;
			money=-1*money;
		}
		if(expwalletid != null){
			expwallet = walletDao.findById(expwalletid);
			bill.setExpwalletid(expwalletid);
			bill.setExpbalance(expwallet.getBalance()-money);//出账后余额
			expwallet.setBalance(expwallet.getBalance()-money);
			walletDao.update(expwallet);//更新钱包余额
		}
		
		if(inwalletid != null){
			inwallet = walletDao.findById(inwalletid);
			bill.setInwalletid(inwalletid);
			bill.setInbalance(inwallet.getBalance()+money);//入账后余额
			inwallet.setBalance(inwallet.getBalance()+money);
			walletDao.update(inwallet);//更新余额
		}
		if(type.intValue() == Constant.BILL_TYPE_TASK.intValue()
				|| type.intValue() == Constant.BILL_TYPE_CASH.intValue()){//任务类型
			if(taskid==null){
				result.put("code", -1);
				result.put("message", "任务类型的账单一定要填写任务ID");
				return result;
			}
			bill.setTaskid(taskid);
			bill.setChiltaskid(chiltaskid);
		}
		//后台操作的需要加入后台操作人员的ID
		bill.setOptuserid(optUserid);
		
		bill.setAmount(money);
		bill.setType(type);
		bill.setRemark(remark);
		bill.setChargechannel(chargechannel);
		bill.setCreatetime(new Date());
		billDao.save(bill);
		result.put("code", 0);
		result.put("message", "创建成功");
		result.put("bill", bill);
		return result;
	}

	@Override
	public void updateBill(Integer id, String data) {
		String hql = "from Bill where expwalletid="+id
				+" and taskid='"+data+"'";
		String ceid = data.split("=")[0];
		List<Bill> bli = billDao.findByHQL(hql);
		if(bli != null && bli.size()>0){
			Bill bill = bli.get(0);
			bill.setTaskid(ceid+"="+Constant.CE_STATE_SUCCESS);
			//bill.setCreatetime(new Date());
			billDao.update(bill);
		}
	}

}
